<template>
  <div class="detail">
    <el-card>
      <el-button v-if="active!='frist'" @click="printInfo">打印</el-button>
      <el-tabs v-model="active">
        <el-tab-pane label="登陆账户设置" name="frist">
          <el-form :model="form" label-width="100px">
            <el-form-item label="姓名">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="form.password" type="password" />
            </el-form-item>
            <div class="footer">
              <el-form-item>
                <el-button type="primary" @click="saveUserInfo">更新</el-button>
                <el-button type="text">取消</el-button>
              </el-form-item>
            </div>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second"><vuserInfo :user-info="userInfo" /></el-tab-pane>
        <el-tab-pane label="岗位信息" name="third"><Jobinfo /></el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
// 导入获取员工基本信息，保存员工信息接口
import { employeesjibenInfo, saveemployeesInfo } from '@/api/employees'

export default {
  components: {
    vuserInfo: () => import('@/views/employees/components/user-info.vue'),
    Jobinfo: () => import('@/views/employees/components/job-info.vue')
  },
  data() {
    return {
      active: 'frist',
      form: {
        username: '',
        password: ''
      },
      userInfo: {}
    }
  },
  created() {
    // 获取员工基本信息
    this.getUserInfo()
  },
  methods: {
    // 打印跳转
    printInfo() {
      this.$router.push(`/employees/print/${this.$route.query.id}?type=${this.active}`)
    },
    // 获取员工基本信息
    async getUserInfo() {
      const res = await employeesjibenInfo(this.$route.query.id)
      // 保存员工信息
      this.userInfo = res
      // 用户名和密码
      this.form.username = res.username
      this.form.password = res.password
    },
    // 保存员工信息
    async saveUserInfo() {
      try {
        const res = await saveemployeesInfo({
          ...this.userInfo,
          ...this.form
        })
        console.log(res)
        this.$message.success('更新成功')
        // 无记录跳回员工页面
        this.$router.replace('/employees')
      } catch {
        console.log('出错了')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card{
  margin: 20px;
  padding: 20px;
   .detail{
        .el-form{
            margin: 50px auto;
            width: 500px;
        }
    }
}
</style>
